/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;

import java.util.List;
import java.util.Date;
import service.IItemService;
import service.IPointService;
import service.IUserService;
import pojo.Item;
import pojo.Pointdispatchhistory;
import pojo.User;

/**
 *
 * @author administrator
 */
public class ItemAction extends ActionSupport {

    private IItemService<Item> itemService;
    private IPointService<Pointdispatchhistory> pointService;
    private IUserService<User> userService;
    private List<Item> items;
    private List<Pointdispatchhistory> points;
    private User user;
    private int now = 1;                 //for rewards
    private int now2 = 1;                //for points
    private int pageSize = 2;            //for rewards
    private int pageSize2 = 10;          //for points
    private int rowCount;                //for rewards
    private int rowCount2;               //for points
    private int pageCount = 1;           //for rewards
    private int pageCount2 = 1;          //for points
    private int type;
    private int itemID;

    public int getItemID() {
        return itemID;
    }

    public void setItemID(int itemID) {
        this.itemID = itemID;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public IUserService<User> getUserService() {
        return userService;
    }

    public void setUserService(IUserService<User> userService) {
        this.userService = userService;
    }


    public IPointService<Pointdispatchhistory> getPointService() {
        return pointService;
    }

    public void setPointService(IPointService<Pointdispatchhistory> pointService) {
        this.pointService = pointService;
    }

    public List<Pointdispatchhistory> getPoints() {
        return points;
    }

    public void setPoints(List<Pointdispatchhistory> points) {
        this.points = points;
    }

    public int getPageCount2() {
        return pageCount2;
    }

    public void setPageCount2(int pageCount2) {
        this.pageCount2 = pageCount2;
    }

    public int getRowCount2() {
        return rowCount2;
    }

    public void setRowCount2(int rowCount2) {
        this.rowCount2 = rowCount2;
    }

    public int getNow2() {
        return now2;
    }

    public void setNow2(int now2) {
        this.now2 = now2;
    }

    public int getPageSize2() {
        return pageSize2;
    }

    public void setPageSize2(int pageSize2) {
        this.pageSize2 = pageSize2;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public IItemService<Item> getItemService() {
        return itemService;
    }

    public void setItemService(IItemService<Item> itemService) {
        this.itemService = itemService;
    }

    public List<Item> getItems() {
        return items;
    }

    public void setItems(List<Item> items) {
        this.items = items;
    }

    public int getNow() {
        return now;
    }

    public void setNow(int now) {
        this.now = now;
    }

    public int getPageCount() {
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getRowCount() {
        return rowCount;
    }

    public void setRowCount(int rowCount) {
        this.rowCount = rowCount;
    }
    private int current_points = 0;

    public int getCurrent_points() {
        return current_points;
    }

    public void setCurrent_points(int current_points) {
        this.current_points = current_points;
    }

    public String listItems() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession(false);
        this.user = this.userService.findUserByUni(session.getAttribute("uni").toString());
        current_points = this.user.getPoints();
        this.rowCount = this.itemService.count();
        this.rowCount2 = this.pointService.count(session.getAttribute("uni").toString());
        int offset = this.calOffset(this.now, this.rowCount, 1, this.pageSize);
        this.calOffset(1, this.rowCount2, 2, this.pageSize2);
        this.items = this.itemService.listItem(offset, this.pageSize);
        this.points = this.pointService.listPoint(session.getAttribute("uni").toString(), 0, this.pageSize2);
        this.type = 1;
        if (this.pageCount == 0) {
            this.pageCount = 1;
        }
        if (this.pageCount2 == 0) {
            this.pageCount2 = 1;
        }
        this.user = this.userService.findUserByUni(session.getAttribute("uni").toString());
        return "listInfo";
    }
    
    public String rewardPage(){
        return "rewardPage";
    }

    public String listPoints() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession(false);
        this.user = this.userService.findUserByUni(session.getAttribute("uni").toString());
        current_points = this.user.getPoints();
        this.rowCount = this.itemService.count();
        this.rowCount2 = this.pointService.count(session.getAttribute("uni").toString());
        this.calOffset(1, this.rowCount, 1, this.pageSize);
        int offset = this.calOffset(this.now2, this.rowCount2, 2, this.pageSize2);
        this.items = this.itemService.listItem(0, this.pageSize);
        this.points = this.pointService.listPoint(session.getAttribute("uni").toString(), offset, this.pageSize2);
        this.type = 2;
        if (this.pageCount == 0) {
            this.pageCount = 1;
        }
        if (this.pageCount2 == 0) {
            this.pageCount2 = 1;
        }
        this.user = this.userService.findUserByUni(session.getAttribute("uni").toString());
        this.type = 2;
        return "listInfo";
    }

    public int calOffset(int theNow, int row, int mode, int size) {
        int page = (row + size - 1) / size;
        if (mode == 1) {
            this.pageCount = page;
        } else {
            this.pageCount2 = page;
        }
        if (theNow < 1) {
            theNow = 1;
        }
        if (theNow > page) {
            theNow = page;
        }
        int offset = (theNow - 1) * size;
        if (offset < 0) {
            offset = 0;
        }
        return offset;
    }

    public String redeem()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession(false);
        this.user = this.userService.findUserByUni(session.getAttribute("uni").toString());
        this.current_points = this.user.getPoints();
        Item item = itemService.getItem(this.itemID);
        if(item.getItemAmount() <= 0||item.getItemPrice() > this.current_points)
            return "error";
        this.current_points = this.current_points - item.getItemPrice();
        item.setItemAmount(item.getItemAmount() - 1);
        itemService.updateItem(item);
        Integer id = this.changePoint(-item.getItemPrice(), "change an item, name: " + item.getItemName());
        this.userService.sendOrderMail(this.user, id.toString(),item.getItemName());
        return this.listPoints();
    }

    public int changePoint(int value,String reason)
    {
        this.userService.increasePoint(this.user, value);
        Pointdispatchhistory point = new Pointdispatchhistory();
        point.setPointDispatchAmount(value);
        point.setPointDispatchDate(new Date());
        point.setPointDispatchReason(reason);
        point.setUser(this.user);
        this.pointService.createNewHistory(point);
        return point.getPointDispatchId();
    }
}
